package me.zhanshi123.jdbcconnectionbridge.sqlbuilder;

public class SQLBuilder {
    private BuilderMode builderMode;

    /**
     * SQLBuilder的构造方法
     *
     * @param builderMode 语句类型
     * @param table       被操作的表
     */
    public SQLBuilder(BuilderMode builderMode, String table) {
        this.builderMode = builderMode;
        this.table = table;
    }

    private String column;

    /**
     * 设置SELECT下想要获取的数据所在的列
     *
     * @param column 单个列的名称
     * @return
     */
    public SQLBuilder result(String column) {
        this.column = "`" + column + "`";
        return this;
    }

    /**
     * 设置SELECT下想要获取的数据所在的列
     *
     * @param columns 多个列的String[]
     * @return
     */
    public SQLBuilder result(String[] columns) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < columns.length; i++) {
            builder.append("`").append(columns[i]).append("`");
            if (i != columns.length - 1) {
                builder.append(",");
            }
        }
        this.column = builder.toString();
        return this;
    }


    private String table;

    /**
     * 设置你需要操作的表
     *
     * @param table 被操作的表
     * @return
     */
    public SQLBuilder table(String table) {
        this.table = table;
        return this;
    }

    private String where;

    /**
     * 设置Raw WHERE条件
     *
     * @param where 原始的WHERE条件SQL
     * @return
     */
    public SQLBuilder whereRaw(String where) {
        this.where = where;
        return this;
    }

    /**
     * 设置WHERE条件
     *
     * @param columns 条件所在列
     * @param values  条件所需的值，可以用'?'来占位
     * @return
     */
    public SQLBuilder where(String[] columns, String[] values) {
        if (columns.length != values.length) {
            throw new IllegalArgumentException("length of column and values don't match");
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            builder.append("`").append(columns[i]).append("` = ");
            if (values[i].equalsIgnoreCase("?")) {
                builder.append("?");
            } else {
                builder.append("'").append(values[i]).append("'");
            }
            if (i != values.length - 1) {
                builder.append(" AND ");
            }
        }
        this.where = builder.toString();
        return this;
    }

    private String order = null;

    /**
     * 设置倒序排列
     *
     * @param column 需要倒序排列的列
     * @return
     */
    public SQLBuilder desc(String column) {
        order = "ORDER BY `" + column + "` DESC";
        return this;
    }

    /**
     * 设置正序排列
     *
     * @param column 需要正序排列的列
     * @return
     */
    public SQLBuilder asc(String column) {
        order = "ORDER BY `" + column + "` ASC";
        return this;
    }

    private String limit = null;

    /**
     * 设置limit
     *
     * @param limit limit值
     * @return
     */
    public SQLBuilder limit(int limit) {
        this.limit = "LIMIT " + limit;
        return this;
    }

    private String set = null;


    /**
     * 设置set的原始SQL
     *
     * @param set set的原始SQL
     * @return
     */
    public SQLBuilder setRaw(String set) {
        this.set = set;
        return this;
    }

    /**
     * 设置set的数据
     *
     * @param columns 需要设置的列
     * @param values  需要设置的值，可以用'?'来占位
     * @return
     */
    public SQLBuilder set(String[] columns, String[] values) {
        if (columns.length != values.length) {
            throw new IllegalArgumentException("length of column and values don't match");
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            builder.append("`").append(columns[i]).append("` = ");
            if (values[i].equalsIgnoreCase("?")) {
                builder.append("?");
            } else {
                builder.append("'").append(values[i]).append("'");
            }
            if (i != values.length - 1) {
                builder.append(",");
            }
        }
        this.set = builder.toString();
        return this;
    }


    /**
     * 设置insert的数据
     *
     * @param columns 数据的列
     * @param values  数据的值，可以用'?'来占位
     * @return
     */
    public SQLBuilder insert(String[] columns, String[] values) {
        if (columns.length != values.length) {
            throw new IllegalArgumentException("length of column and values don't match");
        }
        StringBuilder columnBuilder = new StringBuilder();
        StringBuilder valueBuilder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            if (values[i].equalsIgnoreCase("?")) {
                valueBuilder.append(values[i]);
            } else {
                valueBuilder.append("'").append(values[i]).append("'");
            }
            columnBuilder.append("`").append(columns[i]).append("`");
            if (i != values.length - 1) {
                columnBuilder.append(",");
                valueBuilder.append(",");
            }
        }
        this.insertValues = valueBuilder.toString();
        this.insertColumns = columnBuilder.toString();
        return this;
    }

    private String insertColumns = null;

    /**
     * 设置insert列的原始SQL
     *
     * @param insertColumns insert列的原始SQL
     * @return
     */
    private SQLBuilder insertColumns(String insertColumns) {
        this.insertColumns = insertColumns;
        return this;
    }

    private String insertValues;

    /**
     * 设置insert值的原始SQL
     *
     * @param insertValues insert值的原始SQL
     * @return
     */
    private SQLBuilder insertValues(String insertValues) {
        this.insertValues = insertValues;
        return this;
    }

    /**
     * 生成SQL语句
     * @return 被生成的SQL语句
     */
    public String build() {
        StringBuilder stringBuilder = new StringBuilder();
        if (builderMode == BuilderMode.SELECT) {
            if (column == null) {
                throw new IllegalArgumentException("result() missing!");
            }
            stringBuilder.append("SELECT ").append(column).append(" FROM `").append(table);
            if (where != null) {
                stringBuilder.append("` WHERE ").append(where);
            }
            if (order != null) {
                stringBuilder.append(" ").append(order);
            }
            if (limit != null) {
                stringBuilder.append(" ").append(limit);
            }
        } else if (builderMode == BuilderMode.UPDATE) {
            if (set == null) {
                throw new IllegalArgumentException("set() missing!");
            }
            stringBuilder.append("UPDATE `").append(table).append("` SET ").append(set);
            if (where != null) {
                stringBuilder.append(" WHERE ").append(where);
            }
        } else if (builderMode == BuilderMode.DELETE) {
            stringBuilder.append("DELETE FROM `").append(table).append("`");
            if (where != null) {
                stringBuilder.append(" WHERE ").append(where);
            }
        } else if (builderMode == BuilderMode.INSERT) {
            stringBuilder.append("INSERT INTO `").append(table).append("`(").append(insertColumns).append(") VALUES(").append(insertValues).append(")");
            if (insertColumns == null || insertValues == null) {
                throw new IllegalArgumentException("no insert data!");
            }
        } else {
            return null;
        }
        return stringBuilder.append(";").toString();
    }

    @Override
    public String toString() {
        return build();
    }
}
